<?php

namespace App\Kuafu\Shop\Resources\merchant;

use App\Http\Resources\BaseResource;
use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\Shop\Enum\CodeEnum as ShopEnum;

class ShopListResource extends BaseResource
{
    public static array $collectShopIds = [];

    const TIPS_COMMON          = 0; // 正常 - 不显示
    const TIPS_PREPARE         = 1; // 筹备中
    const TIPS_PREPARE_CHANGE  = 2; // 迁址筹备中
    const TIPS_AGAINST_WARNING = 3; // 违约消息 - 警告
    const TIPS_AGAINST_PUNISH  = 4; // 违约消息 - 处罚
    const RENEW_ORDERS         = 10; // 续签订单消息类型

    public function toArray($request): array
    {
        $tipsType = self::TIPS_COMMON;
        $tipsNum  = 0;
        $tipsId   = 0;

        $status             = $this->status ?? ShopEnum::SHOP_STATUS_NORMAL;
        $nowStepStatus      = $this->nowStepStatus ?? 999;
        $childLocationOrder = $this->childLocationOrder ?? 0;
        $against            = $this->against ?? [];

        // 筹备中
        if ($status == ShopEnum::SHOP_STATUS_PREPARE && $nowStepStatus != 999) {
            $tipsType = self::TIPS_PREPARE;
            $tipsNum  = 1;
            $tipsId   = $request->id;
        }

        // 迁址筹备中
        if ($childLocationOrder == 1 && $nowStepStatus != 999) {
            $tipsType = self::TIPS_PREPARE_CHANGE;
            $tipsNum  = 1;
            $tipsId   = $request->id;
        }

        // 违约
        if (count($against) > 0) {

            if ($against[0]['level'] == 1) {
                $tipsType = self::TIPS_AGAINST_WARNING;
            } else {
                $tipsType = self::TIPS_AGAINST_PUNISH;
            }

            $tipsNum = count($against);
            $tipsId  = $against[0]['id'];
        }

//        // TODO 临时兼容
//        if ($this->status == ShopEnum::SHOP_STATUS_PREPARE) {
//            $tipsType = self::TIPS_PREPARE;
//            $tipsNum  = 1;
//            $tipsId = $request->id;
//        }

        $pendingMsgGroup = [];
        $renewOrders     = $request->renewOrders;
        if ($nowRenewOrder = $renewOrders->where("shopId", $this->id)->first()) {
            array_push($pendingMsgGroup, [
                "type"    => self::RENEW_ORDERS,
                "msg"     => "您有1家门店待续签，请点击查看详情 >",
                "linkUrl" => attributeGet($nowRenewOrder, "linkUrl", "")
            ]);
        }

        return [
            'pendingMsgGroup' => $pendingMsgGroup,
            'id'              => (string)$this->id,
            'isCollected'     => in_array($this->id, self::$collectShopIds),
            'shopCode'        => CommonService::zeroFill($this->shopCode),
            'name'            => str_replace(["夸父炸串", "(", ")", "（", "）"], "", $this->name),
            'address'         => $this->address,
            'prepare'         => (int)($this->status == ShopEnum::SHOP_STATUS_PREPARE), // 废弃
            'tipsType'        => $tipsType,
            'tipsNum'         => $tipsNum,
            'tipsId'          => $tipsId == 0 ? '' : $tipsId,
        ];
    }
}
